#include "server.h"

server::server(QWidget* parent)
    : QMainWindow(parent)
    , ui(new Ui_server)
{
    ui->setupUi(this);

    serversocket = new QTcpServer;
    serversocket->listen(QHostAddress::AnyIPv4, PORT);

    connect(serversocket, &QTcpServer::newConnection, this, &server::new_connection);

}

server::~server()
{
    delete ui; 
}

void server::new_connection()
{
    QTcpSocket* socket = serversocket->nextPendingConnection();
    socket->peerAddress();
    socket->peerPort();
    ui->lineEdit->setText(socket->peerAddress().toString());
    ui->lineEdit_2->setText(QString::number(socket->peerPort())); 
    connect(socket, &QTcpSocket::readyRead, this, &server::client_info_slot);

}

void server::client_info_slot()
{
    QTcpSocket* data = qobject_cast<QTcpSocket*>(sender());
    ui->lineEdit_3->setText(data->readAll());
}
